package com.ai.util;


import com.ai.common.constant.CommonArgs;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
public class Base64Util {

    /**
     * 文件转Base64
     *
     * @param filePath 文件路径
     * @return base64字符串
     * @throws Exception
     */
    public static String GetFileStr(String filePath) throws Exception {
        InputStream in = new FileInputStream(filePath);
        byte[] dataBytes = new byte[in.available()];
        in.read(dataBytes);
        in.close();
        return Base64.encodeBase64String(dataBytes);
    }

    /**
     * Base64转文件
     *
     * @param folder     文件夹
     * @param base64Str  base64字符串
     * @param suffix     后缀（例如：jpg,png,mp4等），不需要小数点
     * @param cameraCode 摄像头代号
     * @param functionType 功能代号
     * @param state 记录状态
     *
     * @return webAPP 相对路径
     */
    public static String GenerateFile(String folder, String base64Str, String suffix, String cameraCode, int functionType, int state) {
        String path = CommonArgs.TOMCAT_PATH;
        String relativePath = "/images" + folder + "/" + functionType + "/" + cameraCode + "/" + getPathByDay() + state + "/";

        try {
            if (base64Str == null) {
                return null;
            } else {
                byte[] bytes = Base64.decodeBase64(base64Str);

                for (int i = 0; i < bytes.length; ++i) {
                    if (bytes[i] < 0) {
                        bytes[i] = (byte) (bytes[i] + 256);
                    }
                }

                String fileName = getFileRandomName() + "." + suffix;
                String savePath = path + relativePath;
                File dir = new File(savePath);
                if (!dir.exists()) {
                    dir.mkdirs();
                }

                OutputStream out = new FileOutputStream(savePath + fileName);
                out.write(bytes);
                out.flush();
                out.close();
//                log.info("-------------------" + savePath + "/" + fileName);
                return relativePath + fileName;
            }
        } catch (IOException var11) {
            return "saveError";
        }
    }

    /**
     * 重命名
     *
     * @return 新名称
     */
    static String getFileRandomName() {
        String day = new SimpleDateFormat("HHmmss").format(new Date());
        String fileName;
        try {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            fileName = day + MD5Util.getMD5Code(uuid);
        } catch (Exception var2) {
            fileName = Long.valueOf(System.currentTimeMillis()).toString();
            log.error("文件重命名失败！错误信息：", var2);
        }

        return fileName;
    }

    /**
     * 生成  "年/月/" 字符串
     *
     * @return
     */
    public static String getPathByMonth() {
        String month = new SimpleDateFormat("yyyy/MM/").format(new Date());
        return month;
    }

    /**
     * 生成  "年/月/日/" 字符串
     *
     * @return
     */
    public static String getPathByDay() {
        String day = new SimpleDateFormat("yyyy/MM/dd/").format(new Date());
        return day;
    }

    public static void main(String[] args) {
    }
}
